import 'package:shared_preferences/shared_preferences.dart';

class PrefUtil {

  static SharedPreferences _sp;

  /// 进入 App 就加载
  static init() async {
    _sp = await SharedPreferences.getInstance();
  }

  //--------------------------------- set -----------------------------------

  /// 如果 value 为 null 则会删除该缓存
  static Future saveString (String key, String value) async{
    await _sp.setString(key, value);
  }

  static Future saveInt (String key, int value) async{
    await _sp.setInt(key, value);
  }

  static Future saveDouble (String key, double value) async{
    await _sp.setDouble(key, value);
  }

  static Future saveBoolean (String key, bool value) async{
    await _sp.setBool(key, value);
  }

  static Future saveStringList (String key, List<String> list) async{
    await _sp.setStringList(key, list);
  }


  //--------------------------------- get -----------------------------------

  static String getString (String key){

    return _sp.getString(key)==null?"":_sp.getString(key);
  }

  static int getInt (String key){
    return _sp.getInt(key);
  }

  static double getDouble (String key){
    return _sp.getDouble(key);
  }

  static bool getBoolean (String key){
    return _sp.getBool(key)??false;
  }

  static List<String> getStringList(String key){
    return _sp.getStringList(key);
  }
}